package handler

import (
	"context"
	"video_server/basic/config"
	"video_server/basic/model"
	pb "video_server/proto"
)

func (s *Server) HomeDisplay(_ context.Context, in *pb.HomeDisplayReq) (*pb.HomeDisplayResp, error) {
	var user model.User
	err := config.DB.Debug().Where("id = ?", in.Uid).Limit(1).Find(&user).Error
	if err != nil {
		return &pb.HomeDisplayResp{
			Message: "用户未登录",
			Code:    10001,
		}, nil
	}
	var work []model.Work
	err = config.DB.Debug().Find(&work).Error
	if err != nil {
		return &pb.HomeDisplayResp{
			Message: "首页数据获取失败",
			Code:    10002,
		}, nil
	}
	var list []*pb.HomeDisplayList
	for _, m := range work {
		list = append(list, &pb.HomeDisplayList{
			WorkId:       m.WorkId,
			CreateUser:   m.CreateUser,
			LiveCount:    m.LiveCount,
			CommentCount: m.CommentCount,
			CollectCount: m.CollectCount,
			ForwardCount: m.ForwardCount,
			LiveStatus:   m.LiveStatus,
		})
	}
	return &pb.HomeDisplayResp{
		Message: "首页数据获取成功",
		Code:    200,
		List:    list,
	}, nil
}
func (s *Server) CreateWork(_ context.Context, in *pb.CreateWorkReq) (*pb.CreateWorkResp, error) {
	var user model.User
	err := config.DB.Debug().Where("id = ?", in.Uid).Limit(1).Find(&user).Error
	if err != nil {
		return &pb.CreateWorkResp{
			Message: "用户未登录",
			Code:    10001,
		}, nil
	}
	var work interface{}
	switch in.WorkType {
	case "图文":
		work = model.Image{
			ImageTitle:  in.Title,
			ImageInfo:   in.Info,
			ImageUrl:    in.Url,
			ImageStatus: in.Status,
			CreateUser:  in.Uid,
		}
	case "音频":
		work = model.Music{
			MTitle:     in.Title,
			MInfo:      in.Info,
			MUrl:       in.Url,
			MStatus:    in.Status,
			CreateUser: in.Uid,
		}
	case "视频":
		work = model.Video{
			VTitle:     in.Title,
			VInfo:      in.Info,
			VUrl:       in.Url,
			VStatus:    in.Status,
			CreateUser: in.Uid,
		}
	case "话题":
		work = model.Subject{
			Title:      in.Title,
			CreateUser: in.Uid,
		}
	default:
		return &pb.CreateWorkResp{
			Message: "没有该选项,请重新选择",
			Code:    10002,
		}, nil
	}
	err = config.DB.Debug().Create(&work).Error
	if err != nil {
		return &pb.CreateWorkResp{
			Message: "作品发布失败",
			Code:    10003,
		}, nil
	}
	return &pb.CreateWorkResp{
		Message: "作品发布成功",
		Code:    200,
	}, nil
}
